#!/bin/bash -ile

#构建版本号
VERSION=`grep '<version>' pom.xml |awk -F'>' '{print $2}'|awk -F"<" '{print $1}'|head -n 1`

#产物全称
Project=springboot-docker-$VERSION.jar

#Jenkins任务名称
JobName=springboot-docker

#站点部署路径
SiteRoot=/www/server/project/$JobName

#构建产物路径
WorkspaceRoot=/var/jenkins_home/workspace

echo "正在拷贝构建的产物·······"
#存放位置
if [ ! -d "$SiteRoot" ];then
  mkdir -p $SiteRoot
  else
  echo "站点路径已经存在！"
fi

#文件存在则删除
if [ ! -f "$SiteRoot/$Project" ];then
  echo "$Project 镜像JAR包不存在！"
  else
  rm -f $SiteRoot/$Project
fi

pids=`ps -ef | grep "$Project" | grep java |awk '{print $2}'`
if [ -z "$pids" ]
then
   echo -e "正在停止占用的进程······\c"
   for pid in $pids ; do
   kill $pid > /dev/null 2>&1
   done
   echo "杀死占用进程完成！"
fi

echo "正在复制JAR至指定站点(为后期做挂载用)："
cp $WorkspaceRoot/$JobName/target/$Project $SiteRoot/$Project
echo "复制产物完成！"

cd $SiteRoot/
BUILD_ID=dontKillMe
echo "正在执行部署中·······"
nohup java -jar $Project > system.log 2>&1 &
echo "自动化部署完成！"